/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * 后序遍历
 * @param {*} root
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxProduct = function (root) {
  let sum,
    avg,
    max = 0;
  // 求树的中位数，两周
  const postOrder = (node) => {
    if (!node) {
      return 0;
    }
    const left = postOrder(node.left);

    const right = postOrder(node.right);
    if (sum) {
      max = Math.max(left * (sum - left), max, right * (sum - right));
    }
    return left + right + node.val;
  };
  sum = postOrder(root);
  avg = sum / 2;
  postOrder(root);
  return max % (10 ** 9 + 7);
};
